package com.ywdnf.gomoku.configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
import java.util.concurrent.TimeUnit;


/**
 * 线程池
 *
 * @author lss
 * @date 2018/10/4, Thu
 */

public class ThreadPoolExecutorFactory {

  /**
   * CORE_POOL_SIZE 池中所保存的线程数，包括空闲线程。
   */
  private static final int CORE_POOL_SIZE = 5;
  /**
   * MAXIMUM_POOL_SIZE - 池中允许的最大线程数(采用LinkedBlockingQueue时没有作用)。
   */
  private static final int MAXIMUM_POOL_SIZE = 5;
  /**
   * KEEP_ALIVE_TIME -当线程数大于核心时，此为终止前多余的空闲线程等待新任务的最长时间，线程池维护线程所允许的空闲时间
   */
  private static final int KEEP_ALIVE_TIME = 10;


  /**
   * 线程池对象
   */
  private static ThreadPoolExecutor threadPoolExecutor = null;

  private ThreadPoolExecutorFactory() {
  }

  @SuppressWarnings("AlibabaThreadShouldSetName")
  public static ThreadPoolExecutor getThreadPoolExecutor() {
    if (null == threadPoolExecutor) {
      ThreadPoolExecutor t;
      synchronized (ThreadPoolExecutorFactory.class) {
        t = threadPoolExecutor;
        if (null == t) {
          synchronized (ThreadPoolExecutorFactory.class) {
            t = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                new DiscardOldestPolicy());
          }
          threadPoolExecutor = t;
        }
      }
    }
    return threadPoolExecutor;
  }
}
